package com.pdg.mcplugin.invisiblehand.commandprocessors;

import org.bukkit.command.CommandSender;

import com.pdg.mcplugin.common.ArgumentList;
import com.pdg.mcplugin.common.baseclasses.CommandProcessorBase;
import com.pdg.mcplugin.invisiblehand.InvisibleHand;
import com.pdg.mcplugin.invisiblehand.dataproviders.Commodity;
import com.pdg.mcplugin.invisiblehand.dataproviders.CommodityKey;

public class GetCommandProcessor extends CommandProcessorBase<InvisibleHand> {

	
	
	private static final int argumentCount = 1;
	private static final String messageNotEnoughArguments = "[IH] Not enough get command arguments!";
	private static final String messageInvalidProperty = "[IH] Unknown property: '%s'";
	private static final String messageInvalidCommodity = "[IH] Could not find commodity '%s' in '%s'.";
	private static final String messageSuccess = "[IH] The commodity '%s' in '%s' has a '%s' of '%s'";

	public GetCommandProcessor(InvisibleHand thePlugin) {
		super(thePlugin, new String[]{
				"/ih get - help for the get command",
				"/ih get (commodity-name) (property-name) - gets the (property-name) of the (commodity-name) for the current world",
				"/ih get (commodity-name) (property-name) (world name) - get the (property-name) of the (commodity-name) for (world name)",
				"Properties: material-type-id, material-data, base-price, base-supply, base-demand, supply-factor, demand-factor, mark-up, sales-count, purchase-count, stock, enabled"
		});
	}

	@Override
	protected boolean specialProcessCommand(CommandSender theCommandSender,
			String theSubCommand, ArgumentList theArgumentList) {
		String theCommodityName = theSubCommand;
		String theWorldName = Utilities.getWorldIfPlayer(theCommandSender);
		if(!Utilities.checkArgumentCount(theCommandSender, theWorldName, theArgumentList, argumentCount, messageNotEnoughArguments)){
			return false;
		}
		String thePropertyName = theArgumentList.pop();
		CommodityProperty property = CommodityProperty.parse(thePropertyName);
		if(property==CommodityProperty.INVALID){
			theCommandSender.sendMessage(String.format(messageInvalidProperty,thePropertyName));
		}
		if(theWorldName==null){
			theWorldName = Utilities.getWorldName(theArgumentList);
		}
		CommodityKey theKey = new CommodityKey(theCommodityName,theWorldName);
		Commodity theCommodity = getPlugin().getDataProvider().getCommodityTableProvider().retrieve(theKey);
		if(theCommodity==null){
			theCommandSender.sendMessage(String.format(messageInvalidCommodity,theCommodityName,theWorldName));
		}
		String theValue = null;
		switch(property){
		case MATERIAL_TYPE_ID:
			theValue = String.format("%d",theCommodity.getItemStack().getTypeId());
			break;
		case MATERIAL_DATA:
			theValue = String.format("%d",theCommodity.getItemStack().getData().getData());
			break;
		case BASE_PRICE:
			theValue = String.format("%f",theCommodity.getBasePrice());
			break;
		case BASE_SUPPLY:
			theValue = String.format("%f",theCommodity.getBaseSupply());
			break;
		case BASE_DEMAND:
			theValue = String.format("%f",theCommodity.getBaseDemand());
			break;
		case SUPPLY_FACTOR:
			theValue = String.format("%f",theCommodity.getSupplyFactor());
			break;
		case DEMAND_FACTOR:
			theValue = String.format("%f",theCommodity.getDemandFactor());
			break;
		case MARK_UP:
			theValue = String.format("%f%%",theCommodity.getMarkUp());
			break;
		case SALES_COUNT:
			theValue = String.format("%d",theCommodity.getSalesCount());
			break;
		case PURCHASE_COUNT:
			theValue = String.format("%d",theCommodity.getPurchaseCount());
			break;
		case STOCK:
			theValue = String.format("%d",theCommodity.getStock());
			break;
		case ENABLED:
			theValue = String.format("%b",theCommodity.getEnabled());
			break;
		default:
			break;
		}
		theCommandSender.sendMessage(String.format(messageSuccess,theCommodityName,theWorldName,property.getLabel(),theValue));
		return true;
	}

}
